package net.wanho.customerManage.dao.impl;

import net.wanho.commons.exception.DaoException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.util.JDBCTemplate;
import net.wanho.customerManage.dao.LinkManDao;
import net.wanho.customerManage.po.LinkMan;
import net.wanho.customerManage.query.LinkManQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.util.List;

public class LinkManDaoImpl implements LinkManDao {
    private JDBCTemplate jdbcTemplate = BeanFactory.getBean(JDBCTemplate.class) ;

    @Override
    public List<LinkMan> selectAllLinkMan() throws DaoException {
        final String SELECT_ALL_LINK_MAN_SQL = "SELECT \tLINK_MAIN_ID LINKMAINID, \n" +
                "\tNAME, \n" +
                "\tSEX, \n" +
                "\tPOSITION, \n" +
                "\tNICKNAME, \n" +
                "\tPHONENUM, \n" +
                "\tEMAIL, \n" +
                "\tQQ, \n" +
                "\tCREATE_TIME CREATETIME, \n" +
                "\tUPDATE_TIME UPDATETIME, \n" +
                "\tREMARKS, \n" +
                "\tQR_CODE QRCODE, \n" +
                "\tCUSTOMER_ID CUSTOMERID\n" +
                "\t \n" +
                "\tFROM \n" +
                "link_man " ;
        try {
            return jdbcTemplate.queryForList(SELECT_ALL_LINK_MAN_SQL,LinkMan.class);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }

    @Override
    public LinkMan selectByIdLinkMan(Integer linkMainId) throws DaoException {
        final String SELECT_BY_ID_LINK_MAN_SQL = "SELECT \tLINK_MAIN_ID LINKMAINID, \n" +
                "\tNAME, \n" +
                "\tSEX, \n" +
                "\tPOSITION, \n" +
                "\tNICKNAME, \n" +
                "\tPHONENUM, \n" +
                "\tEMAIL, \n" +
                "\tQQ, \n" +
                "\tCREATE_TIME CREATETIME, \n" +
                "\tUPDATE_TIME UPDATETIME, \n" +
                "\tREMARKS, \n" +
                "\tQR_CODE QRCODE, \n" +
                "\tCUSTOMER_ID CUSTOMERID\n" +
                "\t \n" +
                "\tFROM \n" +
                "link_man " +
                " where  LINK_MAIN_ID = ?" ;
        try {
            return jdbcTemplate.queryForObject(SELECT_BY_ID_LINK_MAN_SQL,LinkMan.class,linkMainId);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }

    @Override
    public List<LinkMan> selectByCondLinkMan(LinkManQuery linkManQuery) throws DaoException {
        final String SELECT_BASE_LINK_MAN_SQL = "SELECT \tLINK_MAIN_ID LINKMAINID, \n" +
                "\tNAME, \n" +
                "\tSEX, \n" +
                "\tPOSITION, \n" +
                "\tNICKNAME, \n" +
                "\tPHONENUM, \n" +
                "\tEMAIL, \n" +
                "\tQQ, \n" +
                "\tCREATE_TIME CREATETIME, \n" +
                "\tUPDATE_TIME UPDATETIME, \n" +
                "\tREMARKS, \n" +
                "\tQR_CODE QRCODE, \n" +
                "\tCUSTOMER_ID CUSTOMERID\n" +
                "\t \n" +
                "\tFROM \n" +
                "link_man where 1=1  " ;
        try {
            Object[] params = ArrayUtils.EMPTY_OBJECT_ARRAY ;
            StringBuffer sb = new StringBuffer(SELECT_BASE_LINK_MAN_SQL);
            if (StringUtils.isNotEmpty(linkManQuery.getEmail())){
                sb.append(" and EMAIL like ? ") ;
                params = ArrayUtils.add(params,"%"+linkManQuery.getEmail()+"%") ;
            }
            if (StringUtils.isNotEmpty(linkManQuery.getMobile())){
                sb.append(" and PHONENUM like ? ") ;
                params = ArrayUtils.add(params,"%"+linkManQuery.getMobile()+"%") ;
            }
            return jdbcTemplate.queryForList(sb.toString(),LinkMan.class,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }

    @Override
    public Integer insertLinkMan(LinkMan linkMan) throws DaoException {
        final String INSERT_LINK_MAN_SQL = "INSERT INTO crm.link_man " +
                "(LINK_MAIN_ID, NAME, SEX, POSITION, NICKNAME, PHONENUM, EMAIL, QQ, CREATE_TIME, UPDATE_TIME, REMARKS, QR_CODE, CUSTOMER_ID)" +
                "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ;
        try {
            Object[] params = {linkMan.getLinkMainId(),linkMan.getName(),linkMan.getSex(),linkMan.getPosition(),
                    linkMan.getNickname(),linkMan.getPhonenum(),linkMan.getEmail(),linkMan.getQq(),
                    linkMan.getCreateTime(),linkMan.getUpdateTime(),linkMan.getRemarks(),
                    linkMan.getQrode(),linkMan.getCustomerId()} ;
            return jdbcTemplate.save(INSERT_LINK_MAN_SQL,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }

    @Override
    public void updateLinkMan(LinkMan linkMan) throws DaoException {
        final String UPDATE_LINK_MAN_SQL = "UPDATE link_man SET NAME = ? ,SEX = ? ,POSITION = ? ,NICKNAME = ? ," +
                "PHONENUM = ? ,EMAIL = ? ,QQ = ? ,UPDATE_TIME = ? ,REMARKS = ? ," +
                "QR_CODE = ? ,CUSTOMER_ID = ? WHERE LINK_MAIN_ID = ?" ;
        try {
            Object[] params = {linkMan.getName(),linkMan.getSex(),linkMan.getPosition(),
                    linkMan.getNickname(),linkMan.getPhonenum(),linkMan.getEmail(),linkMan.getQq(),
                    linkMan.getUpdateTime(),linkMan.getRemarks(),
                    linkMan.getQrode(),linkMan.getCustomerId(),linkMan.getLinkMainId()} ;
           jdbcTemplate.update(UPDATE_LINK_MAN_SQL,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }
}
